package com.ruoyi.algorithm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.algorithm.entity.Image;
import com.ruoyi.algorithm.entity.Manufacturer;
import com.ruoyi.algorithm.entity.Operator;
import com.ruoyi.algorithm.model.ListManufacturerRequestModel;
import com.ruoyi.algorithm.model.ManufacturerDDBModel;
import com.ruoyi.algorithm.repository.ImageRepository;
import com.ruoyi.algorithm.repository.ManufacturerRepository;
import com.ruoyi.algorithm.repository.OperatorRepository;
import com.ruoyi.algorithm.service.ManufacturerService;
import com.ruoyi.algorithm.util.MybatisUtils;
import com.beihu.sightplus.common.authorization.service.UserAuthorization;
import com.beihu.sightplus.common.response.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

@Service
public class ManufacturerServiceImpl implements ManufacturerService {
    private Logger logger = Logger.getLogger(ManufacturerServiceImpl.class.getName());
    @Resource
    private ManufacturerRepository manufacturerRepository;

    @Resource
    private OperatorRepository operatorRepository;

    @Resource
    private ImageRepository imageRepository;

    /**
     * 查询厂商下拉框数据
     */
    public List<ManufacturerDDBModel> getManufacturerDDBModelList() {
        List<ManufacturerDDBModel> list = new ArrayList<>();
        List<Manufacturer> manufacturerList = manufacturerRepository.list();
        for (Manufacturer manufacturer : manufacturerList) {
            ManufacturerDDBModel manufacturerDDBModel = new ManufacturerDDBModel();
            manufacturerDDBModel.setManufacturerId(manufacturer.getId());
            manufacturerDDBModel.setManufacturerName(manufacturer.getName());
            list.add(manufacturerDDBModel);
        }
        return list;
    }

    /**
     * 添加厂商
     *
     * @param manufacturer
     * @return
     */
    @Override
    public boolean addManufacturer(Manufacturer manufacturer) {
        manufacturer.setDelFlag("0");
        manufacturer.setCreateTime(new Date());
        manufacturer.setCreateUser(UserAuthorization.getUserAuthorization().getId());
        return manufacturerRepository.save(manufacturer);
    }

    /**
     * 删除厂商
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteManufacturer(Long id) {

        // 查询是否关联算子
        int countOperator = operatorRepository.count(Wrappers.lambdaQuery(Operator.class)
                .eq(Operator::getDelFlag, "0")
                .eq(Operator::getManufacturerId, id));
        if (countOperator > 0) {
            logger.info("该商家有算子关联无法删除");
            return false;
        }

        // 查询是否关联镜像
        int countImage = imageRepository.count(Wrappers.lambdaQuery(Image.class)
                .eq(Image::getDelFlag, "0")
                .eq(Image::getManufacturerId, id));

        if (countImage > 0) {
            logger.info("该商家有镜像关联无法删除");
            return false;
        }

        Manufacturer manufacturer = new Manufacturer();
        manufacturer.setId(id);
        manufacturer.setUpdateTime(new Date());
        manufacturer.setDelFlag("1");
        boolean result = manufacturerRepository.updateById(manufacturer);
        return result;
    }

    /**
     * 修改厂商
     *
     * @param manufacturer
     * @return
     */
    @Override
    public boolean updateManufacturer(Manufacturer manufacturer) {
        manufacturer.setUpdateTime(new Date());
        manufacturer.setUpdateUser(UserAuthorization.getUserAuthorization().getId());
        return manufacturerRepository.updateById(manufacturer);
    }

    /**
     * 通过id查询厂商
     *
     * @param id
     * @return
     */
    @Override
    public Manufacturer getById(Long id) {
        Manufacturer manufacturer = manufacturerRepository.getOne(Wrappers.lambdaQuery(Manufacturer.class)
                .eq(Manufacturer::getId, id));
        return manufacturer;
    }

    /**
     * 分页查询厂商
     *
     * @param model
     * @return
     */
    @Override
    public PageResult listManufacturer(ListManufacturerRequestModel model) {
        PageResult pageResult = new PageResult();
        String jsonString = JSON.toJSONString(model);
        Manufacturer manufacturer = JSONObject.parseObject(jsonString, Manufacturer.class);
        QueryWrapper<Manufacturer> queryWrapper = Wrappers.query(manufacturer)
                .eq("del_flag", "0");

        // 构建排序条件
        MybatisUtils.getOrderBy(queryWrapper, model);

        // 分页条件
        Page manufacturerPage = new Page<Manufacturer>();
        manufacturerPage.setSize(model.getSize());
        manufacturerPage.setCurrent(model.getCurrent());

        // 分页查询
        Page<Manufacturer> resultPage = manufacturerRepository.page(manufacturerPage, queryWrapper);
        pageResult.setContent(resultPage.getRecords());
        pageResult.setTotalPages(resultPage.getPages());
        pageResult.setTotalSize(resultPage.getTotal());

        return pageResult;
    }


}
